/**
 * 
 */
package com.googlecode.flair.icons;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.PrintWriter;

/**
 * @author ccPascu
 *
 */
public class IconGaleryGrabber {
	
	public static void main(String[] args) {
		File galery = new File("d:/DESIGN/icons/tango-icon-theme-0.8.1/16x16");
		File[] categories = galery.listFiles(new FileFilter( ) {
			public boolean accept(File file) {
				return file.isDirectory();
			}
		});
		
		for (File categoryFolder : categories) {
			File[] categoryImageFiles = categoryFolder.listFiles(new FilenameFilter() {
				public boolean accept(File dir, String name) {
					return name.endsWith(".png");
				}
			});
			String categoryName = categoryFolder.getName();
			writeCategoryClass(categoryName, categoryImageFiles, "16x16");
			writeCategoryClass(categoryName, categoryImageFiles, "22x22");
			writeCategoryClass(categoryName, categoryImageFiles, "32x32");
		}
	}
	
	private static void writeCategoryClass(String categoryName, File[] categoryImageFiles, String size) {
		final String className = "Tango" + Character.toUpperCase(categoryName.charAt(0)) + categoryName.substring(1) + size;
		final File classFile = new File("D:/OPENSOURCE/myprojects/flair/FlairIcons/src/com/googlecode/flair/icons/tango", 
				className + ".as");
		try {
			PrintWriter writer = new PrintWriter(classFile);
			writer.printf("package com.googlecode.flair.icons.tango\n" + 
					"{\n" + 
//					"\n" + 
//					"\timport com.googlecode.flair.icons.IImageGalery;\n" +
					"\n" +
					"\tpublic class %s \n" +
					"\t{\n\n", className);
						
			System.out.println(" --- " + className + " --- ");
			final StringBuilder sb = new StringBuilder();
			int items = 0;
			for (File imageFile : categoryImageFiles) {
				final String fileName = imageFile.getName().substring(0, imageFile.getName().indexOf('.'));
				final String varName = fileName.replace('-', '_');
				writer.printf("\t\t[Embed(source='/assets/tango/%s/%s/%s.png')]\n" + 
						"\t\tprivate static var %s : Class;\n\n", size, categoryName, fileName, varName);
				if (sb.length() > 0) {
					sb.append(", ");
					if (items %10 == 0) {
						sb.append("\n");
					}
				}
				sb.append(varName);
				items++;
			}
			
			writer.print("\t\tpublic static icons : Array = [" + sb.toString() + "];\n\n");
			writer.print("\t}\n" +
					"}");
			writer.flush();
			writer.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
